package belf.migrate.api.table.schema;

import belf.migrate.api.exception.CatalogException;
import belf.migrate.api.util.JdbcUrlUtil;

/**
 * 目标数据库Catalog的基类，用于封装实现过程相同的方法，以减少{@link AbstractJdbcCatalog}的复杂度
 */
public abstract class AbstractSinkCatalog extends AbstractJdbcCatalog {
    private static final String QUOTE = "\"";

    public AbstractSinkCatalog(String catalogName,
                               String username,
                               String pwd,
                               JdbcUrlUtil.UrlInfo urlInfo,
                               String defaultSchema,
                               boolean caseSensitive) {
        super(catalogName, username, pwd, urlInfo, defaultSchema, caseSensitive);
    }

    @Override
    public String getQuote() {
        return QUOTE;
    }

    @Override
    public String getUrlFromDatabaseName(String databaseName) {
        return defaultUrl;
    }

    /**
     * 对数据库进行默认查询的SQL
     * @param tablePath
     * @return
     */
    @Override
    protected String getSelectColumnsSql(TablePath tablePath) {
        return "SELECT * FROM " + tablePath.getFullName();
    }

    @Override
    public String getDropTableSql(TablePath tablePath) {
        return "DROP TABLE " + tablePath.getFullName() + " CASCADE";
    }

    @Override
    public String getDropViewSql(TablePath viewPath) {
        return String.format("DROP VIEW IF EXISTS %s CASCADE",
                viewPath.getFullNameWithQuoted());
    }

    @Override
    public boolean dropView(TablePath viewPath) throws CatalogException {
        return executeInternal(defaultUrl, getDropViewSql(viewPath));
    }

    @Override
    public boolean dropTrigger(TablePath triggerPath) throws CatalogException {
        String sql = String.format("DROP TRIGGER IF EXISTS %s CASCADE",
                triggerPath.getFullName());
        return executeInternal(defaultUrl, sql);
    }

    @Override
    public void dropType(TablePath typePath) throws CatalogException {
        String sql = String.format("DROP TYPE %s CASCADE", typePath.getFullName());
        executeSql(typePath, sql);
    }

    @Override
    public void dropSequence(TablePath seqPath) throws CatalogException {
        String sql = String.format("DROP SEQUENCE %s CASCADE", seqPath.getFullName());
        executeSql(seqPath, sql);
    }

    @Override
    public void dropFunction(TablePath functionPath) throws CatalogException {
        String sql = String.format("DROP FUNCTION %s CASCADE", functionPath.getFullName());
        executeSql(functionPath, sql);
    }

    @Override
    public void dropSynonym(TablePath synonymPath) throws CatalogException {
        // FIXME: 注意补齐对公有同义词的删除处理：是否需要加关键词“PUBLIC”
        String sql = String.format("DROP SYNONYM %s", synonymPath.getFullName());
        executeSql(synonymPath, sql);
    }

    @Override
    public void dropProcedure(TablePath procedurePath) throws CatalogException {
        String sql = String.format("DROP PROCEDURE %s CASCADE", procedurePath.getFullName());
        executeSql(procedurePath, sql);
    }
}
